package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.auth.User;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IUserDao;
import cn.edu.sjtu.se.dslab.exception.DBException;

@Repository("userDao")
public class UserDaoImpl implements IUserDao {
	
	private static final String PASSWORD = "password";
	private static final String USERNAME = "username";
	private static final String REALNAME = "realname";
	private static final String STAFFNUM = "staffNum"; 
	
	@Resource(name = "baseDao")
	private IBaseDao<User> baseDao;

	@Override
	public void save(User user) {
		baseDao.save(user);
	}

	@Override
	public void update(User user) {
		baseDao.update(user);
	}


	@Override
	public User validUser(String username, String password) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from User where ");
		hql.append(USERNAME + "='" + username + "' and ");
		hql.append(PASSWORD + "='" + password + "'");
		List<User> resultList = baseDao.query(hql.toString());
		if (resultList != null && resultList.size() > 0) {
			return resultList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public long rowCount() {
		// TODO Auto-generated method stub
		short status = -1;
		return baseDao.countAll("User", status, null);
	}

	@Override
	public User queryById(long id) {
		// TODO Auto-generated method stub
		return baseDao.queryById(User.class, id);
	}

	@Override
	public User queryByUserName(String username) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from User a where ");
		hql.append("a.username='" + username + "' ");
		System.out.println(hql.toString());
		List<User> resultList = baseDao.query(hql.toString());
		if (resultList != null && resultList.size() > 0) {
			return resultList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void delete(User user) {
		// TODO Auto-generated method stub
		baseDao.delete(user);
	}

	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		baseDao.delById(User.class, id);
	}

	@Override
	public List<User> queryAll() {
		// TODO Auto-generated method stub
		return baseDao.queryList("User");
	}

	@Override
	public PageBean<User> queryByPage(int pageNum, int pageSize) throws DBException{
		// TODO Auto-generated method stub
		PageBean<User> bean = baseDao.queryByHqlForPage(pageNum, pageSize, "User");
		return bean;
	}

	@Override
	public List<User> queryByProperty(String text) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from User where ");
		hql.append(USERNAME + " like '%" + text + "%' or ");
		hql.append(REALNAME + " like '%" + text + "%' or ");
		hql.append(STAFFNUM + " like '%" + text + "%' ");
		List<User> resultList = baseDao.query(hql.toString());
		return resultList;
	}
}
